using BasicScript.Component;
using Game.Core;
using Game.Main.Model;
using Game.Main.View;
using Game.Manager;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class SitLieBeDItem : BaseItem
	{
		private enum State
		{
			Lie = 0,
			Sit = 1
		}

		private const float DIF_Y = 0.25f;

		private State state;

		private Transform btnConfirm;

		private bool isChanging;

		private bool isChangeToSit;

		private FrameAnimation backAnim;

		private FrameAnimation frontAnim;

		private CharacterItem character;

		private BodyDataItem bodyDataDisplayer;

		private Transform sitPoint;

		private Transform liePoint;

		private float curY;

		private float targetY;

		private float timer;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			btnConfirm = model.Find("BtnConfirm");
			btnConfirm.gameObject.layer = 15;
			frontAnim = model.Find("Front").GetComponent<FrameAnimation>();
			backAnim = model.Find("Back").GetComponent<FrameAnimation>();
			liePoint = model.Find("LiePoint");
			sitPoint = model.Find("SitPoint");
			sitPoint.localPosition = new Vector3(liePoint.localPosition.x, liePoint.localPosition.y + 0.852f + 0.25f, liePoint.localPosition.z);
			SetBedState((State)data.state);
		}

		protected override void LateUpdate()
		{
			if (!isChanging)
			{
				return;
			}
			timer += Time.deltaTime * 4f;
			if (character != null)
			{
				character.transform.SetLocalY(Mathf.Lerp(curY, targetY, timer));
			}
			if (timer >= 1f)
			{
				isChanging = false;
				if (character != null)
				{
					character.SetColliderEnabled(true);
					character = null;
				}
			}
		}

		public override bool AttachItem(BaseItem item, DepthType depthType, AttachType attachType, bool updatePosition)
		{
			bool result = base.AttachItem(item, depthType, attachType, updatePosition);
			CheckDisplayerState();
			return result;
		}

		public override void ReleaseAttachItem(BaseItem item)
		{
			base.ReleaseAttachItem(item);
			CheckDisplayerState();
		}

		private void CheckDisplayerState()
		{
			if (bodyDataDisplayer == null && !string.IsNullOrEmpty(base.itemData.scriptParams))
			{
				BaseItem itemById = Singleton<Scene>.Instance.GetItemById(base.itemData.scriptParams);
				if (itemById != null)
				{
					bodyDataDisplayer = (BodyDataItem)itemById;
				}
			}
			if (bodyDataDisplayer != null)
			{
				if (childItems.Count > 0)
				{
					bodyDataDisplayer.IsShowNum(true);
				}
				else
				{
					bodyDataDisplayer.IsShowNum(false);
				}
			}
		}

		public override void TouchDown(TouchEventData touchEventData)
		{
			base.TouchDown(touchEventData);
			if (isChanging)
			{
				return;
			}
			RaycastHit2D raycastHit2D = Physics2D.Raycast(touchEventData.curPos, Vector2.zero, 15f, 32768);
			if (!(raycastHit2D.collider != null) || !(raycastHit2D.collider.transform == btnConfirm))
			{
				return;
			}
			isChanging = true;
			character = null;
			if (childItems.Count > 0 && childItems[0].itemData.scriptType == 1)
			{
				character = (CharacterItem)childItems[0];
				character.SetColliderEnabled(false);
			}
			PlayChangeSound();
			if (state == State.Sit)
			{
				if (character != null)
				{
					ReleaseAttachItem(character);
					AttachItem(character, DepthType.Front, AttachType.LieCenter, false);
					curY = character.transform.localPosition.y;
					targetY = 0f;
				}
				state = State.Lie;
				frontAnim.PlayReversed();
				backAnim.PlayReversed();
			}
			else
			{
				if (character != null)
				{
					ReleaseAttachItem(character);
					AttachItem(character, DepthType.Front, AttachType.Sit, false);
					curY = character.transform.localPosition.y;
					targetY = -0.852f;
				}
				state = State.Sit;
				frontAnim.Play();
				backAnim.Play();
			}
			timer = 0f;
		}

		private void PlayChangeSound()
		{
			if (base.itemData.quicklyClickSound != null && base.itemData.quicklyClickSound.Length == 2)
			{
				if (state == State.Sit)
				{
					Singleton<AudioManager>.Instance.PlaySound(base.itemData.quicklyClickSound[0]);
				}
				else
				{
					Singleton<AudioManager>.Instance.PlaySound(base.itemData.quicklyClickSound[1]);
				}
			}
		}

		public override void PlayQuicklyClickSound()
		{
		}

		private void SetBedState(State val)
		{
			if (state != val)
			{
				state = val;
				if (state == State.Lie)
				{
					frontAnim.GotoFirstFrame();
					backAnim.GotoFirstFrame();
				}
				else
				{
					frontAnim.GotoLastFrame();
					backAnim.GotoLastFrame();
				}
			}
		}

		public override bool CanLie()
		{
			if (state == State.Sit || isChanging)
			{
				return false;
			}
			return base.CanLie();
		}

		public override bool CanSit()
		{
			if (state == State.Lie || isChanging)
			{
				return false;
			}
			return base.CanLie();
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["33"] = (int)state;
		}
	}
}
